package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.redis.service.IUserFavorRedisService;
import cn.wolfcode.wolf2w.util.RedisKeysStandard;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserFavorRedisServiceImpl implements IUserFavorRedisService {
    @Autowired
    private StringRedisTemplate template;
    @Override
    public List<String> getUserId(String pattern) {
        String p = RedisKeysStandard.USER_FAVOR_STRATEGIES.addKeyMessage(pattern);
        Set<String> keys = template.keys(p);
        List<String> list = new ArrayList<>();
        if (keys != null && keys.size() > 0){
            for (String key : keys) {
                String[] split = key.split(":");
                list.add(split[1]);
            }
        }
        return list;
    }

    @Override
    public List<String> getFavorList(String uid) {
        List<String> sidList = new ArrayList<>();
        String key = RedisKeysStandard.USER_FAVOR_STRATEGIES.addKeyMessage(uid);
        if (template.hasKey(key)) {
            String sidStr = template.opsForValue().get(key);
            sidList = JSON.parseArray(sidStr,String.class);
        }
        return sidList;
    }

    @Override
    public boolean isExist(String key) {
        return template.hasKey(key);
    }

    @Override
    public void setUserFavor(String key, String value) {
        template.opsForValue().set(key,value);
    }
}
